package mtm;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

    public static void main(String[] args) {
        // Logger
        try {
            Logger.getLogger("").addHandler(new FileHandler("errorlog.xml"));
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Initialisation and Properties
        Vars.PropPath = System.getProperty("user.home") + "/.mtm";
        Vars.PropFileName = Vars.PropPath + "/Options.xml";
        File dir = new File(Vars.PropPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Vars.PluginsPath = Vars.PropPath + "/Plugins";
        dir = new File(Vars.PluginsPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        Vars.DataPath = Vars.PropPath + "/Data";
        dir = new File(Vars.DataPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        Vars.LoadProperties();
        if (!Vars.prop.containsKey("PluginsToLoad")) {
            Vars.prop.setProperty("PluginsToLoad", ";");
            Vars.SaveProperties();
        }
        if (!Vars.prop.containsKey("http.proxyHost")) {
            Vars.prop.setProperty("http.proxyHost", "null");
            Vars.SaveProperties();
        } else {
            if (!Vars.prop.getProperty("http.proxyHost").equals("null")) {
                System.setProperty("http.proxyHost", Vars.prop.getProperty("http.proxyHost"));
            }
        }
        if (!Vars.prop.containsKey("http.proxyPort")) {
            Vars.prop.setProperty("http.proxyPort", "null");
            Vars.SaveProperties();
        } else {
            if (!Vars.prop.getProperty("http.proxyPort").equals("null")) {
                System.setProperty("http.proxyPort", Vars.prop.getProperty("http.proxyPort"));
            }
        }
        // End of Initialisation and Properties

        Vars.LoadPlugins();

        // Look and feel
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        MainFrame frm = new MainFrame();
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }
}
